from flask import Flask, jsonify, request
from flask_cors import CORS
from matchTools import acupointTool, diseTool, sympTool, medicineTool
from queryTool import queryTool
from graphQueryTool import graphQueryTool

app = Flask(__name__)
CORS(app)


def main_agent(question: str):
    acu = acupointTool(question)
    dis = diseTool(question)
    symp = sympTool(question)
    med = medicineTool(question)
    reference = queryTool(question, acu, dis, symp, med)
    query = f"{question}\n{reference}"
    response = graphQueryTool(query)
    return {
        "response": response
    }


@app.route("/api/agent", methods=['POST'])
def agent_api():
    try:
        data = request.get_json()
        if not data or 'question' not in data:
            return jsonify({"error": "Missing 'question' field in JSON"}), 400

        question = data['question']
        result = main_agent(question)
        return jsonify(result)

    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)
